package com.bw.a2209clubshop.ui.main

import android.content.Intent
import android.os.Bundle
import android.widget.RadioButton
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.get
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import com.alibaba.android.arouter.facade.annotation.Route
import com.bw.a2209clubshop.R
import com.bw.a2209clubshop.base.BaseActivity
import com.bw.a2209clubshop.databinding.ActivityMainBinding
import com.bw.a2209clubshop.mmkv.AuthMMKV
import com.bw.a2209clubshop.ui.club.ClubHomeFragmentV2
import com.bw.a2209clubshop.ui.club.MotorClubNotJoinedFragment
import com.bw.a2209clubshop.ui.main.viewModel.MainViewModel
import com.bw.a2209clubshop.ui.mall.view.MallFragment
import com.bw.a2209clubshop.ui.message.MessageFragment
import com.bw.a2209clubshop.ui.mine.MineFragment
import com.bw.a2209clubshop.ui.moto_circle.MotoCircleV3Fragment
import com.bw.a2209clubshop.utils.Router
import com.tencent.mmkv.BuildConfig
import dagger.hilt.android.AndroidEntryPoint

@Route(path = Router.Ui.MainActivity)
@AndroidEntryPoint
class MainActivity : BaseActivity<MainViewModel,ActivityMainBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_main
    private val mViewModel:MainViewModel by viewModels()
    companion object {
        private const val MY_CAR_FRAGMENT = "myCar"
        private const val MOTOR_CIRCLE_FRAGMENT = "friends"
        private const val MOTOR_CLUB_FRAGMENT = "club"
        private const val MOTOR_CLUB_FRAGMENT_CHECKING = "club_checking"
        private const val MOTOR_CLUB_FRAGMENT_NOT_JOINED = "club_not_joined"
        private const val MINE_FRAGMENT = "mine"
        private const val MALL_FRAGMENT = "mall"
        private const val MESSAGE_FRAGMENT = "message"

        const val MESSAGE_REGISTRATION_ID = "com.bw.a2209clubshop.cyclone.MESSAGE_REGISTRATION_ID"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }
    private val mMotoCircleFragment by lazy {
        // MotoCircleV2Fragment()
        MotoCircleV3Fragment()
    }


    private val mMotoClubNotJoinedFragment: Fragment by lazy {
        if (BuildConfig.DEBUG)
            ClubHomeFragmentV2()
        else
            MotorClubNotJoinedFragment()
    }
    private val mMineFragment by lazy {
        MineFragment()
    }
    private val mMallFragment by lazy {
        MallFragment()
    }
    private val mMessageFragment by lazy {
        MessageFragment()
    }
    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        // 从其他地方返回主页时，有切换TAB的需求时 使用
        val pos = intent?.getIntExtra("POSITION", -1) ?: -1
        if (pos != -1) {
            mBinding.rgBottom[0].performClick()
        }
    }

    override fun initListener() {
        super.initListener()
        mBinding.rgBottom.setOnCheckedChangeListener { _, i ->
            navToFragment(i)
        }
    }

//    private fun updateUnreadMessage() {
//        if (AuthMMKV.token.isNotEmpty())
//            mViewModel.getUnreadMessageV2()
//    }

    override fun initView() {
        super.initView()
        //默认进入玩车页面
        navToFragment(R.id.nav_motor_circle)
        (mBinding.rgBottom[1] as RadioButton).isChecked = true
    }

    //底部导航
    private fun navToFragment(id: Int) {
        val bt = supportFragmentManager.beginTransaction()
        hideCurrentFragment(bt)
        when (id) {
            R.id.nav_motor_circle -> {
                if (supportFragmentManager.findFragmentByTag(MOTOR_CIRCLE_FRAGMENT)?.isAdded != true) {
                    bt.add(R.id.fragment_container, mMotoCircleFragment, MOTOR_CIRCLE_FRAGMENT)
                } else {
                    bt.show(mMotoCircleFragment)
                }

                bt.commitAllowingStateLoss()
//                updateUnreadMessage()
            }

            R.id.nav_motor_club -> {
                analysisClubFragment(bt)
            }

            R.id.nav_mine -> {
                if (supportFragmentManager.findFragmentByTag(MINE_FRAGMENT)?.isAdded != true) {
                    bt.add(R.id.fragment_container, mMineFragment, MINE_FRAGMENT)
                } else {
                    bt.show(mMineFragment)
                }
                bt.commitAllowingStateLoss()
//                updateUnreadMessage()
            }

            R.id.nav_motor_mall -> {
                if (supportFragmentManager.findFragmentByTag(MALL_FRAGMENT)?.isAdded != true) {
                    bt.add(R.id.fragment_container, mMallFragment, MALL_FRAGMENT)
                } else {
                    bt.show(mMallFragment)
                }
                bt.commitAllowingStateLoss()
//                updateUnreadMessage()
            }

            R.id.nav_message -> {
                if (supportFragmentManager.findFragmentByTag(MESSAGE_FRAGMENT)?.isAdded != true) {
                    bt.add(R.id.fragment_container, mMessageFragment, MESSAGE_FRAGMENT)
                } else {
                    bt.show(mMessageFragment)
                }
                bt.commitAllowingStateLoss()
//                if (AuthMMKV.token.isEmpty()) {
//                    LoginHelper(this).jumpLogin()
//                }
//                updateUnreadMessage()
            }
        }
    }
    private fun hideCurrentFragment(bt: FragmentTransaction) {
        if (supportFragmentManager.findFragmentByTag(MOTOR_CIRCLE_FRAGMENT)?.isVisible == true) {
            bt.hide(mMotoCircleFragment)
        }
        hideClubFragment(bt)
        if (supportFragmentManager.findFragmentByTag(MINE_FRAGMENT)?.isVisible == true) {
            bt.hide(mMineFragment)
        }
        if (supportFragmentManager.findFragmentByTag(MALL_FRAGMENT)?.isVisible == true) {
            bt.hide(mMallFragment)
        }

        if (supportFragmentManager.findFragmentByTag(MESSAGE_FRAGMENT)?.isVisible == true) {
            bt.hide(mMessageFragment)
        }
    }
    /** 隐藏俱乐部相关Fragment */
    private fun hideClubFragment(bt: FragmentTransaction) {
//        if (supportFragmentManager.findFragmentByTag(MOTOR_CLUB_FRAGMENT_CHECKING)?.isVisible == true) {
//            bt.hide(mMotorClubCheckingFragment)
//        }
//        if (supportFragmentManager.findFragmentByTag(MOTOR_CLUB_FRAGMENT)?.isVisible == true) {
//            bt.hide(mMotoClubFragment)
//        }
        if (supportFragmentManager.findFragmentByTag(MOTOR_CLUB_FRAGMENT_NOT_JOINED)?.isVisible == true) {
            bt.hide(mMotoClubNotJoinedFragment)
        }
    }
    /** 分析俱乐部Fragment */
    private fun analysisClubFragment(bt: FragmentTransaction) {
//        if (SDataProvider.needCheckingClub) {
//            /** 检查俱乐部状态 */
//            if (supportFragmentManager.findFragmentByTag(MOTOR_CLUB_FRAGMENT_CHECKING)?.isAdded != true) {
//                bt.add(R.id.fragment_container, mMotorClubCheckingFragment, MOTOR_CLUB_FRAGMENT_CHECKING)
//            } else {
//                bt.show(mMotorClubCheckingFragment)
//            }
//        } else if (SDataProvider.hasJoinedClub && SDataProvider.autoJumpDetailWhenJoined) {
//            /** 已加入某个俱乐部 */
//            if (supportFragmentManager.findFragmentByTag(MOTOR_CLUB_FRAGMENT)?.isAdded != true) {
//                bt.add(R.id.fragment_container, mMotoClubFragment, MOTOR_CLUB_FRAGMENT)
//            } else {
//                bt.show(mMotoClubFragment)
//            }
//        } else {
        /** 未加入任何俱乐部 */
        if (supportFragmentManager.findFragmentByTag(MOTOR_CLUB_FRAGMENT_NOT_JOINED)?.isAdded != true) {
            bt.add(R.id.fragment_container, mMotoClubNotJoinedFragment, MOTOR_CLUB_FRAGMENT_NOT_JOINED)
        } else {
            bt.show(mMotoClubNotJoinedFragment)
        }
//        }
        bt.commitAllowingStateLoss()
    }
    private fun isNeedUpdate(new: List<String>, current: List<String>): Boolean {
        if (new[0].toInt() > current[0].toInt()) {
            return true
        } else if (new[0].toInt() < current[0].toInt()) {
            return false
        }
        if (new[1].toInt() > current[1].toInt()) {
            return true
        } else if (new[1].toInt() < current[1].toInt()) {
            return false
        }
        if (new[2].toInt() > current[2].toInt()) {
            return true
        } else if (new[2].toInt() < current[2].toInt()) {
            return false
        }
        return false
    }
    /** 供子Fragment调用的方法 */
    fun replaceClubFragment() {
        val bt = supportFragmentManager.beginTransaction()
        hideClubFragment(bt)
        analysisClubFragment(bt)
    }
}